package com.ml.mall.utils;


import cn.hutool.core.util.ObjectUtil;
import com.ml.mall.bean.res.log.OperationLogQueryRes;
import com.ml.mall.consts.CommonConsts;
import com.ml.mall.entity.log.OperationLog;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.*;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * Created by kyyang on 2021/1/13 20:36
 */
public class ExcelUtils {

    private static void exportData(String fileName, Workbook workbook, HttpServletResponse response) {
        //Workbook workbook = getMlpayMerBalanceChange(res);
        if (workbook != null) {
            ByteArrayOutputStream byteArrayOutputStream = null;

            try {
                byteArrayOutputStream = new ByteArrayOutputStream();
                workbook.write(byteArrayOutputStream);
                String suffix = ".xls";
                response.setContentType("application/vnd.ms-excel;charset=utf-8");
                response.setHeader("Content-Disposition", "attachment;filename=" + new String((fileName + suffix).getBytes(), "iso-8859-1"));
                OutputStream outputStream = response.getOutputStream();
                outputStream.write(byteArrayOutputStream.toByteArray());
                outputStream.close();
            } catch (Exception var19) {
                var19.printStackTrace();
            } finally {
                try {
                    if (byteArrayOutputStream != null) {
                        byteArrayOutputStream.close();
                    }
                } catch (IOException var18) {
                    var18.printStackTrace();
                }

                try {
                    workbook.close();
                } catch (IOException var17) {
                    var17.printStackTrace();
                }

            }
        }
    }



    public static void exportOperationLogExcel(String fileName, List<OperationLogQueryRes> list, HttpServletResponse response) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建sheet
        Sheet sheet = workbook.createSheet("LogDetail");
        sheet.setColumnWidth(0, 26 * 256);
        sheet.setColumnWidth(1, 26 * 256);
        sheet.setColumnWidth(2, 15 * 256);
        sheet.setColumnWidth(3, 15 * 256);
        sheet.setColumnWidth(4, 15 * 256);


        HSSFCellStyle cellStyle = workbook.createCellStyle();
        Font font = workbook.createFont();
        font.setBold(true);
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        cellStyle.setFont(font);
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);

        HSSFCellStyle cellStyle2 = workbook.createCellStyle();
        cellStyle2.setAlignment(HorizontalAlignment.CENTER);
        cellStyle2.setBorderBottom(BorderStyle.THIN);
        cellStyle2.setBorderLeft(BorderStyle.THIN);
        cellStyle2.setBorderRight(BorderStyle.THIN);
        cellStyle2.setBorderTop(BorderStyle.THIN);

        // 表头
        Row row = sheet.createRow(0);
        Cell cell = row.createCell(0);
        cell.setCellValue("Date");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(1);
        cell.setCellValue("User");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(2);
        cell.setCellValue("Roles");
        cell.setCellStyle(cellStyle);
        cell = row.createCell(3);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("Event");
        cell = row.createCell(4);
        cell.setCellStyle(cellStyle);
        cell.setCellValue("Description");



        for (OperationLogQueryRes r : list) {
            Row recordRow = sheet.createRow(sheet.getLastRowNum() + 1);
            Cell cell1 = recordRow.createCell(0);
            cell1.setCellValue(r.getOperCreateTime() != null ? sdf.format(r.getOperCreateTime()) : "");
            cell1.setCellStyle(cellStyle2);

            cell1 =recordRow.createCell(1);
            cell1.setCellStyle(cellStyle2);
            cell1.setCellValue(ObjectUtil.defaultIfNull(r.getOperUserId(), ""));

            cell1 =recordRow.createCell(2);
            cell1.setCellStyle(cellStyle2);
            cell1.setCellValue(r.getRoleName() != null ? r.getRoleName() : "");

            cell1 =recordRow.createCell(3);
            cell1.setCellStyle(cellStyle2);
            cell1.setCellValue(ObjectUtil.defaultIfNull(r.getOperModule(), ""));

            cell1 =recordRow.createCell(4);
            cell1.setCellStyle(cellStyle2);
            cell1.setCellValue(ObjectUtil.defaultIfNull(r.getOperDesc(),""));
        }
        exportData(fileName,workbook,response);
    }
}
